﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties.CApSLPSetLocationsPropertiesLocationsListUtils
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using C1.Win.C1FlexGrid;
using C1.Win.C1FlexGrid.Util.BaseControls;
using EngineeringInternalExtension;
using System.Collections.ObjectModel;
using System.Drawing;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui48;


namespace DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties;

internal class CApSLPSetLocationsPropertiesLocationsListUtils
{
  internal static bool m_shouldFireParamEvents = true;
  private TxFlexGrid _txFGLocationsList;
  private TxObjectList _preSelectedLocations;
  internal static RefreshCommonSourcePropertiesListDueToEvent _eventHandlerReference = (RefreshCommonSourcePropertiesListDueToEvent) null;

  public TxObjectList PreSelectedLocations
  {
    get => this._preSelectedLocations;
    private set => this._preSelectedLocations = value;
  }

  public TxFlexGrid LocationsList => this._txFGLocationsList;

  public CApSLPSetLocationsPropertiesLocationsListUtils(
    TxFlexGrid locationsList,
    RefreshCommonSourcePropertiesListDueToEvent refreshListHandlerReference)
  {
    this.PreSelectedLocations = CApSLPSetLocationsPropertiesLogic.GetSelectedLocations();
    this._txFGLocationsList = locationsList;
    CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference = refreshListHandlerReference;
    this.RegisterToLocationsEvents();
  }

  public void InitializeList() => this.CreateLocationsListStructure();

  private void CreateLocationsListStructure()
  {
    ((C1FlexGridBase) this._txFGLocationsList).Cols[0].Name = ((C1FlexGridBase) this._txFGLocationsList).Cols[0].Caption = StringTable.ResourceManager.GetString("SLP_LocationsListTitle");
  }

  private Image GetTxObjectIcon(ITxObject obj)
  {
    switch (obj)
    {
      case TxRoboticViaLocationOperation _:
      case TxWeldLocationOperation _:
      case TxRoboticSeamLocationOperation _:
        return TxIconManager.the().GetObjectIcon(obj);
      default:
        return (Image) null;
    }
  }

  public void FillLocationsWithContent(TxObjectList locations)
  {
    ((C1FlexGridBase) this._txFGLocationsList).Select(-1, -1);
    ((ScrollableControl) this._txFGLocationsList).BeginUpdate();
    if (((RowColCollection) ((C1FlexGridBase) this._txFGLocationsList).Rows).Count > 1)
      ((C1FlexGridBase) this._txFGLocationsList).Rows.RemoveRange(1, ((RowColCollection) ((C1FlexGridBase) this._txFGLocationsList).Rows).Count - 1);
    foreach (ITxRoboticLocationOperation location in (Collection<ITxObject>) locations)
    {
      Row row = ((C1FlexGridBase) this._txFGLocationsList).Rows.Add();
      row[0] = (object) ((ITxObject) location).Name;
      ((C1FlexGridBase) this._txFGLocationsList).SetCellImage(((RowCol) row).Index, 0, this.GetTxObjectIcon((ITxObject) location));
    }
    ((ScrollableControl) this._txFGLocationsList).EndUpdate();
  }

  private CellStyle TitleStyle()
  {
    CellStyle cellStyle;
    if (((C1FlexGridBase) this._txFGLocationsList).Styles.Contains(nameof (TitleStyle)))
    {
      cellStyle = ((C1FlexGridBase) this._txFGLocationsList).Styles[nameof (TitleStyle)];
    }
    else
    {
      cellStyle = ((C1FlexGridBase) this._txFGLocationsList).Styles.Add(nameof (TitleStyle));
      cellStyle.Font = new Font(cellStyle.Font, FontStyle.Bold);
      cellStyle.TextAlign = (TextAlignEnum) 4;
    }
    return cellStyle;
  }

  internal static void Refresh()
  {
    if (CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference == null)
      return;
    CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference();
  }

  public void RegisterToLocationsEvents()
  {
    foreach (ITxRoboticLocationOperation selectedLocation in (Collection<ITxObject>) this.PreSelectedLocations)
    {
      ((ITxOperation) selectedLocation).DescriptionSet += new TxOperation_DescriptionSetEventHandler(this.location_DescriptionSet);
      ((ITxOperation) selectedLocation).DurationSet += new TxOperation_DurationSetEventHandler(this.location_DurationSet);
      ((ITxRoboticOperation) selectedLocation).MountedWorkpieceOperationStateChanged += new TxRoboticOperation_MountedWorkpieceOperationStateChangedEventHandler(this.location_MountedWorkpieceOperationStateChanged);
      ((ITxObject) selectedLocation).NameChanged += new TxObject_NameChangedEventHandler(this.location_NameChanged);
      ((ITxRoboticOperation) selectedLocation).ParameterRemoved += new TxRoboticOperation_ParameterRemovedEventHandler(this.location_ParameterRemoved);
      ((ITxRoboticOperation) selectedLocation).ParameterSet += new TxRoboticOperation_ParameterSetEventHandler(this.location_ParameterSet);
      selectedLocation.RobotConfigurationSet += new TxRoboticLocationOperation_ConfigurationSetEventHandler(this.location_RobotConfigurationSet);
    }
  }

  public void UnRegisterFromLocationsEvents()
  {
    foreach (ITxRoboticLocationOperation selectedLocation in (Collection<ITxObject>) this.PreSelectedLocations)
    {
      ((ITxOperation) selectedLocation).DescriptionSet -= new TxOperation_DescriptionSetEventHandler(this.location_DescriptionSet);
      ((ITxOperation) selectedLocation).DurationSet -= new TxOperation_DurationSetEventHandler(this.location_DurationSet);
      ((ITxRoboticOperation) selectedLocation).MountedWorkpieceOperationStateChanged += new TxRoboticOperation_MountedWorkpieceOperationStateChangedEventHandler(this.location_MountedWorkpieceOperationStateChanged);
      ((ITxObject) selectedLocation).NameChanged -= new TxObject_NameChangedEventHandler(this.location_NameChanged);
      ((ITxRoboticOperation) selectedLocation).ParameterRemoved -= new TxRoboticOperation_ParameterRemovedEventHandler(this.location_ParameterRemoved);
      ((ITxRoboticOperation) selectedLocation).ParameterSet -= new TxRoboticOperation_ParameterSetEventHandler(this.location_ParameterSet);
      selectedLocation.RobotConfigurationSet -= new TxRoboticLocationOperation_ConfigurationSetEventHandler(this.location_RobotConfigurationSet);
    }
  }

  private void location_RobotConfigurationSet(
    object sender,
    TxRoboticLocationOperation_ConfigurationSetEventArgs args)
  {
    if (CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference == null)
      return;
    CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference();
  }

  private void location_ParameterSet(object sender, TxRoboticOperation_ParameterSetEventArgs args)
  {
    if (CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference == null || !CApSLPSetLocationsPropertiesLocationsListUtils.m_shouldFireParamEvents)
      return;
    CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference();
  }

  private void location_ParameterRemoved(
    object sender,
    TxRoboticOperation_ParameterRemovedEventArgs args)
  {
    if (CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference == null || !CApSLPSetLocationsPropertiesLocationsListUtils.m_shouldFireParamEvents)
      return;
    CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference();
  }

  private void location_NameChanged(object sender, TxObject_NameChangedEventArgs args)
  {
    if (CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference == null)
      return;
    CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference();
  }

  private void location_MountedWorkpieceOperationStateChanged(
    object sender,
    TxRoboticOperation_MountedWorkpieceOperationStateChangedEventArgs args)
  {
    if (CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference == null)
      return;
    CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference();
  }

  private void location_DurationSet(object sender, TxOperation_DurationSetEventArgs args)
  {
    if (CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference == null)
      return;
    CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference();
  }

  private void location_DescriptionSet(object sender, TxOperation_DescriptionSetEventArgs args)
  {
    if (CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference == null)
      return;
    CApSLPSetLocationsPropertiesLocationsListUtils._eventHandlerReference();
  }
}
